const fs = require('fs');
const glob = require("glob")
const os = require('os');
const path = require('path')

const homedir = os.homedir();
const buffDir = path.resolve(homedir, 'webpack')

!fs.existsSync(buffDir) && fs.mkdirSync(buffDir)

var fileBuff = {};

const fbuff = path.resolve(buffDir, 'buff.json')
if (fs.existsSync(fbuff)) {
  let data = fs.readFileSync(fbuff, {encoding:'utf-8'})
  fileBuff = JSON.parse(data);
}

function isModified(file) {
    let fstat = fs.statSync(file)
    //console.log(fstat)
    let basename = path.basename(file)
    let key = basename.replace('.', '_')
    let last = fileBuff[key]
    let mtimeMs = fstat.mtime.getTime()
    fileBuff[key] = mtimeMs
    fs.writeFileSync(fbuff, JSON.stringify(fileBuff))
    console.log(basename, mtimeMs != last)
    return mtimeMs != last
}

module.exports = {
    isModified
}
